using System;
using System.Media;
using System.IO;

namespace F1RacingGame
{
    public class SoundManager
    {
        private static SoundManager instance;
        private bool isEnabled = true;
        
        // 音效播放器
        private SoundPlayer engineSound;
        private SoundPlayer collisionSound;
        private SoundPlayer checkpointSound;
        private SoundPlayer menuSound;
        
        private SoundManager() { }
        
        public static SoundManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SoundManager();
                }
                return instance;
            }
        }
        
        public bool IsEnabled
        {
            get { return isEnabled; }
            set { isEnabled = value; }
        }
        
        public void Initialize()
        {
            try
            {
                // 初始化音效播放器
                // 注意：这里我们使用简单的系统音效，实际项目中可以使用更复杂的音频库
                engineSound = new SoundPlayer();
                collisionSound = new SoundPlayer();
                checkpointSound = new SoundPlayer();
                menuSound = new SoundPlayer();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"音效初始化失败: {ex.Message}");
                isEnabled = false;
            }
        }
        
        public void PlayEngineSound()
        {
            if (!isEnabled) return;
            
            try
            {
                // 播放引擎声音（使用系统默认音效）
                SystemSounds.Asterisk.Play();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"播放引擎音效失败: {ex.Message}");
            }
        }
        
        public void PlayCollisionSound()
        {
            if (!isEnabled) return;
            
            try
            {
                // 播放碰撞声音
                SystemSounds.Hand.Play();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"播放碰撞音效失败: {ex.Message}");
            }
        }
        
        public void PlayCheckpointSound()
        {
            if (!isEnabled) return;
            
            try
            {
                // 播放检查点声音
                SystemSounds.Question.Play();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"播放检查点音效失败: {ex.Message}");
            }
        }
        
        public void PlayMenuSound()
        {
            if (!isEnabled) return;
            
            try
            {
                // 播放菜单声音
                SystemSounds.Beep.Play();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"播放菜单音效失败: {ex.Message}");
            }
        }
        
        public void Dispose()
        {
            engineSound?.Dispose();
            collisionSound?.Dispose();
            checkpointSound?.Dispose();
            menuSound?.Dispose();
        }
    }
} 